package com.zdf.license.creator.config;

import com.zdf.license.core.service.AServerInfos;
import com.zdf.license.creator.model.LicenseCreatorParam;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

@ConfigurationProperties(prefix = "license.generate")
public class LicenseCreatorProperties {

    /**证书生成临时存放路径*/
    private String tempPath = "license/temp/creator";
    private String filename = "license.lic";

    private String subject = "cspt";
    private String privateAlias = "privateKey";
    private String keyPass = "wsn++123";
    private String storePass = "wsn++123";
    private String privateKeysStorePath = "/privateKeys.store";

    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getPrivateAlias() {
        return privateAlias;
    }
    public void setPrivateAlias(String privateAlias) {
        this.privateAlias = privateAlias;
    }
    public String getKeyPass() {
        return keyPass;
    }
    public void setKeyPass(String keyPass) {
        this.keyPass = keyPass;
    }
    public String getStorePass() {
        return storePass;
    }
    public void setStorePass(String storePass) {
        this.storePass = storePass;
    }
    public String getPrivateKeysStorePath() {
        return privateKeysStorePath;
    }
    public void setPrivateKeysStorePath(String privateKeysStorePath) {
        this.privateKeysStorePath = privateKeysStorePath;
    }
    public String getTempPath() {
        return tempPath;
    }
    public String getFilename() {
        return filename;
    }
    public void setFilename(String filename) {
        this.filename = filename;
    }
    public void setTempPath(String tempPath) {
        this.tempPath = tempPath;
        File file = new File(tempPath);
        if(!file.exists()){
            file.mkdirs();
        }
    }

    public String getLicensePath() throws Exception {
        //设置格式
        SimpleDateFormat format =  new SimpleDateFormat("yyyyMMddHHmmss");
        if(tempPath == null || "".equals(tempPath)){
            // 如果默认临时文件等于空的话，就获取当前服务执行的路径
            tempPath = AServerInfos.getServerTempPath();
        }
        // 根据时间戳，命名lic文件
        File licenseFile = new File(tempPath, "license");
        File tempFile = new File(licenseFile, format.format(System.currentTimeMillis()));
        if(!tempFile.mkdirs()){
            throw new Exception("创建目录"+tempFile.getPath()+",失败，请检查是是否有创建目录的权限或者手动进行创建！");
        }
        return new File(tempFile, filename).getPath();
    }

    /**
     * 利用系统参数生成license创建参数
     */
    public LicenseCreatorParam buildLicenseCreatorParam() throws Exception {
//        String licensePath = getLicensePath();
        return LicenseCreatorParam.builder()
                .subject(subject)
                .privateAlias(privateAlias)
                .keyPass(keyPass)
                .storePass(storePass)
                .privateKeysStorePath(privateKeysStorePath)
                .issuedTime(new Date())
                .consumerType("user")
                .consumerAmount(1)
                .clientAmount(1)
                .description("内部授权")
//                .licensePath(licensePath)
//                .licUrl("/download?path="+licensePath)
                .build();
    }

}
